package com.peng.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.edu.entity.Teacher;
import com.peng.edu.entity.vo.TeacherQuery;
import com.peng.edu.service.TeacherService;
import com.peng.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-12
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 返回所有讲师信息
     * @return
     */
    @ApiOperation("返回所有讲师信息")
    @GetMapping("/findAll")
    public Result<Map<String, Object>> getAllTeacher(){
        List<Teacher> list = teacherService.list(null);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items",list);
        return Result.ok().data(map);
    }


    /**
     * 按照 id 删除讲师，逻辑删除
     * @param id
     * @return
     */
    @ApiOperation("删除讲师")
    @DeleteMapping("/{id}")
    @CacheEvict(value = "index",allEntries = true)
    public Result removeById(@PathVariable String id){
        teacherService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/pageTeacher/{page}/{limit}")
    public Result pageTeacher(@PathVariable Integer page,@PathVariable Integer limit){
        Page<Teacher> teacherPage = new Page<>(page, limit);
        IPage<Teacher> teacherIPage = teacherService.page(teacherPage, null);
        return Result.ok().data(teacherIPage);
    }

    @ApiOperation("分页查询待条件")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public Result pageTeacherCondition(@PathVariable Integer page,
                                       @PathVariable Integer limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<Teacher> teacherPage = new Page<>(page, limit);
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(teacherQuery.getName()!=null, Teacher::getName,teacherQuery.getName());
        queryWrapper.eq(teacherQuery.getLevel()!=null, Teacher::getLevel,teacherQuery.getLevel());
        queryWrapper.ge(teacherQuery.getBegin()!=null, Teacher::getGmtCreate,teacherQuery.getBegin());
        queryWrapper.le(teacherQuery.getEnd()!=null, Teacher::getGmtModified,teacherQuery.getEnd());
        queryWrapper.orderByDesc(Teacher::getGmtCreate);
        IPage<Teacher> teacherIPage = teacherService.page(teacherPage, queryWrapper);
        return Result.ok().data(teacherIPage);
    }

    /**
     * 添加讲师
     * @param讲师信息
     * @return
     */
    @ApiOperation("添加讲师")
    @CacheEvict(value = "index",allEntries = true)
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        return teacherService.save(teacher)? Result.ok(): Result.error();
    }

    @ApiOperation("根据 id 查询讲师")
    @GetMapping("/getTeacherById/{id}")
    public Result getTeacherById(@ApiParam("讲师 id") @PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("item",teacher);
        return Result.ok().data(map);
    }

    @ApiOperation("根据 id 修改讲师")
    @CacheEvict(value = "index",allEntries = true)
    @PutMapping("/updateTeacherById")
    public Result updateTeacherById(@ApiParam("讲师信息") @RequestBody Teacher eduTeacher) {
        boolean update = teacherService.updateById(eduTeacher);
        return update? Result.ok(): Result.error();
    }
}

